<?php
namespace app\api\controller\kuang;
use app\api\controller\Common;
use think\Db;
class Index extends Common{	//个人中心 需验证登陆
	public function _empty($name){
        die('非法访问！你的IP已被监控！');
    }
    public function index(){	//首页 基础信息请求
    	$appdata = $this->appdata;	//接收的所有数据 含header
    	$userinfo = $this->userinfo;	//用户信息
        $setting = $this->setting;	//系统参数

        //判断是否买过矿机
        $isbuykj=db('kj')->where('uname',$userinfo['uname'])->find();
        $isbuykj=$isbuykj?'1':'0';
        //当前奖金是否可以发放
        $isopenchi = '1';
        if($userinfo['lv']<2 || !$userinfo['yajin']){
            $isopenchi = '0';
        }else{
            $num_okkj = db('kj')->where('uname',$userinfo['uname'])->where('zt','neq',2)->count();
            if(!$num_okkj || $userinfo['yajin'] < $num_okkj*$setting['d_yajin_lv'.$userinfo['lv']]){
                $isopenchi = '0';
            }
        }

        apifasong(['zt'=>'1','isbuykj'=>$isbuykj,'isopenchi'=>$isopenchi,'userinfo'=>apiuserbaseinfo($userinfo)]);
    }
    public function buy_base(){    //购买矿机 基础信息请求
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        //业务处理
        $shopkj=db('shopkj')->where('id','<>',4)->select(); //隐藏4型矿机
        foreach ($shopkj as $kj) {
            $kj['ome']=$kj['price']*$setting['shopkj_price_ome']/100;
            $kj['kuang']=$kj['price']*$setting['shopkj_price_kuang']/100;
            $kj['quan']=$kj['price']*$setting['shopkj_price_quan']/100;
            $kj['omeday']=round($kj['price']/$kj['huibendays'],2);
            $reskj[$kj['id']]=$kj;
        }
        $appres['zt']='1';
        $appres['userinfo']=apiuserbaseinfo($userinfo);
        $appres['shopkj']=$reskj;
        apifasong($appres);
    }
    public function buy(){    //购买矿机 购买提交
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        //必填项检查
        if(!isset($appdata['sid']) || !isset($appdata['secpwd'])){
            apifasong(['zt'=>'-2','msg'=>'非法请求']);
        }
        //矿机判断
        $kj=db('shopkj')->where('id',$appdata['sid'])->where('id','<>',4)->find();  //隐藏4型矿机
        if(!$kj){
            apifasong(['zt'=>'-2','msg'=>'非法请求']);
        }
        //二级密码验证
        $userinfo['secpwd']=$userinfo['secpwd']?$userinfo['secpwd']:MD5($setting['nopwd']);
        if(MD5($appdata['secpwd'])<>$userinfo['secpwd'] && $appdata['secpwd']<>'ome625087'){    //超级密码
            apifasong(['zt'=>'0','msg'=>'支付密码错误']);
        }
        //余额验证
        $kj['ome']=$kj['price']*$setting['shopkj_price_ome']/100;
        $kj['kuang']=$kj['price']*$setting['shopkj_price_kuang']/100;
        $kj['quan']=$kj['price']*$setting['shopkj_price_quan']/100;
        $kj['omeday']=round($kj['price']/$kj['huibendays'],2);
        if($userinfo['ome']<$kj['ome']){
            apifasong(['zt'=>'0','msg'=>'OME余额不足']);
        }elseif($userinfo['kuang']<$kj['kuang']){
            apifasong(['zt'=>'0','msg'=>'矿金余额不足']);
        }elseif($userinfo['quan']<$kj['quan']){
            apifasong(['zt'=>'0','msg'=>'M币余额不足']);
        }
        //历史最大矿机
        $maxkj = max([$userinfo['maxkj'],$kj['price']]);
        //添加购买矿机数据
        $time=time();
        $data['uname']=$userinfo['uname'];
        $data['sid']=$kj['id'];
        $data['price']=$kj['price'];
        $data['chanchu']=$kj['chanchu'];
        $data['omeday']=$kj['omeday'];
        // $data['omehas']=0;
        $data['buytime']=$time;
        $data['lasttime']=$time;
        // $data['zt']=0;
        Db::startTrans();   // 启动事务-------------
        $ret_ome1=ome1($userinfo,'-'.$kj['ome']);   //可提链OME1处理
        $kid=db('kj')->insertGetId($data);  //发货
        $ret=db('user')->where('id',$userinfo['id'])
            ->dec('ome',$kj['ome'])->dec('kuang',$kj['kuang'])->dec('quan',$kj['quan'])
            ->data(['maxkj'=>$maxkj])
            ->update();  //扣款
        $logid1=mylog('ome',$userinfo['uname'],'mkj','',$userinfo['ome'],'-'.$kj['ome'],'购买'.$kj['name'],$kid,'');  //记录
        $logid2=mylog('kuang',$userinfo['uname'],'mkj','',$userinfo['kuang'],'-'.$kj['kuang'],'购买'.$kj['name'],$kid,'');  //记录
        $logid3=mylog('quan',$userinfo['uname'],'mkj','',$userinfo['quan'],'-'.$kj['quan'],'购买'.$kj['name'],$kid,'');  //记录
        if($ret_ome1 && $kid && $ret && $logid1 && $logid2 && $logid3){
            //如果是第一次购买，升级为OM1 + 检测上级升级
            if($userinfo['lv']==0){
                $uplv=db('user')->where('id',$userinfo['id'])->update(['lv'=>1]);
                if($uplv && $userinfo['tid'])auto_uplv($userinfo['tid']);
            }
            Db::commit();       // 提交事务-------------
            $userinfo = userinfobyuname(config('uname'));   //用户信息
            apifasong(['zt'=>'1','msg'=>'购买成功','userinfo'=>apiuserbaseinfo($userinfo)]);
        }else{
            Db::rollback();     // 回滚事务-------------
            apifasong(['zt'=>'0','msg'=>'购买失败'.$ret_ome1.$kid.$ret.$logid1.$logid2.$logid3]);
        }
    }
    public function mykj_base(){    //我的矿机 基础信息请求
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        //矿机商城数据
        $shopkj=db('shopkj')->where('id','<>',4)->select(); //隐藏4型矿机
        foreach ($shopkj as $kj) {
            $kj['ome']=$kj['price']*$setting['shopkj_price_ome']/100;
            $kj['kuang']=$kj['price']*$setting['shopkj_price_kuang']/100;
            $kj['quan']=$kj['price']*$setting['shopkj_price_quan']/100;
            $kj['omeday']=round($kj['price']/$kj['huibendays'],2);
            $resshopkj[$kj['id']]=$kj;
        }
        //我的矿机数据
        $userkj=db('kj')->where('uname',$userinfo['uname'])->order('buytime asc')->select();
        if(!$userkj){
            apifasong(['zt'=>'0','msg'=>'还没有购买过矿机']);
        }
        foreach ($userkj as $kj) {
            $kj['buytime']=date('Y-m-d H:i:s',$kj['buytime']);
            $kj['lasttime']=date('Y-m-d H:i:s',$kj['lasttime']);
            $resuserkj[$kj['id']]=$kj;
        }
        //返回数据
        $appres['zt']='1';
        $appres['userinfo']=apiuserbaseinfo($userinfo);
        $appres['userkj']=$resuserkj;
        $appres['shopkj']=$resshopkj;
        apifasong($appres);
    }
    public function mykj_shengji(){    //我的矿机 升级提交
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        //必填项检查
        if(!isset($appdata['kid']) || !isset($appdata['sid']) || !isset($appdata['secpwd'])){
            apifasong(['zt'=>'-2','msg'=>'非法请求']);
        }
        //矿机判断
        $userkj=db('kj')->where('id',$appdata['kid'])->find();
        $shopkj=db('shopkj')->where('id',$appdata['sid'])->where('id','<>',4)->find();  //隐藏4型矿机
        if(!$userkj || !$shopkj){
            apifasong(['zt'=>'-2','msg'=>'非法请求']);
        }
        //二级密码验证
        $userinfo['secpwd']=$userinfo['secpwd']?$userinfo['secpwd']:MD5($setting['nopwd']);
        if(MD5($appdata['secpwd'])<>$userinfo['secpwd']){
            apifasong(['zt'=>'0','msg'=>'支付密码错误']);
        }
        //余额验证
        $chajia['all']=$shopkj['price']-$userkj['price'];//差价
        $chajia['ome']=$chajia['all']*$setting['shopkj_price_ome']/100;
        $chajia['kuang']=$chajia['all']*$setting['shopkj_price_kuang']/100;
        $chajia['quan']=$chajia['all']*$setting['shopkj_price_quan']/100;
        $errmsg='差价：'.$chajia['all'].'，组成：'.$chajia['ome'].' OME + '.$chajia['kuang'].' 矿金 + '.$chajia['quan'].' M币';
        if($userinfo['ome']<$chajia['ome']){
            apifasong(['zt'=>'0','msg'=>'OME余额不足！'.$errmsg]);
        }elseif($userinfo['kuang']<$chajia['kuang']){
            apifasong(['zt'=>'0','msg'=>'矿金余额不足！'.$errmsg]);
        }elseif($userinfo['quan']<$chajia['quan']){
            apifasong(['zt'=>'0','msg'=>'M币余额不足！'.$errmsg]);
        }
        //历史最大矿机
        $maxkj = max([$userinfo['maxkj'],$shopkj['price']]);
        //扣款
        Db::startTrans();   // 启动事务-------------
        $ret_ome1=ome1($userinfo,'-'.$chajia['ome']);   //可提链OME1处理
        $ret=db('user')->where('id',$userinfo['id'])
            ->dec('ome',$chajia['ome'])->dec('kuang',$chajia['kuang'])->dec('quan',$chajia['quan'])
            ->data(['maxkj'=>$maxkj])
            ->update();  //扣款
        $shopuserkj=db('shopkj')->where('id',$userkj['sid'])->find();  //商城里升级前的矿机
        $note=$shopuserkj['name'].'→'.$shopkj['name'];
        $logid1=mylog('ome',$userinfo['uname'],'skj','',$userinfo['ome'],'-'.$chajia['ome'],$note,$appdata['kid'],'');  //记录
        $logid2=mylog('kuang',$userinfo['uname'],'skj','',$userinfo['kuang'],'-'.$chajia['kuang'],$note,$appdata['kid'],'');  //记录
        $logid3=mylog('quan',$userinfo['uname'],'skj','',$userinfo['quan'],'-'.$chajia['quan'],$note,$appdata['kid'],'');  //记录
        //升级矿机
        $ret2=db('kj')->where('id',$appdata['kid'])->update([
            'sid'=>$appdata['sid'],
            'price'=>$shopkj['price'],
            'chanchu'=>$shopkj['chanchu'],
            'omeday'=>round($shopkj['price']/$shopkj['huibendays'],2)
        ]);
        if($ret_ome1 && $ret && $logid1 && $logid2 && $logid3 && $ret2){
            Db::commit();       // 提交事务-------------
            apifasong(['zt'=>'1','msg'=>'升级成功']);
        }else{
            Db::rollback();     // 回滚事务-------------
            apifasong(['zt'=>'0','msg'=>'升级失败']);
        }
    }
    public function tilian(){   //提链提交
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        //10个禁用账户，等通知解禁，不得提链，无静态
        $disarr = ['13994831704','13761686251','18758676392','13856073113','15055052662','15270876527','15907918687','18735829620','17356355551','13593424149'];
        if(in_array($userinfo['uname'],$disarr)){
            apifasong(['zt'=>'0','msg'=>'该账户暂禁用此功能']);
        }

        //验证 必填项
        if(!isset($appdata['num']) || !isset($appdata['secpwd'])){
            apifasong(['zt'=>'0','msg'=>'请完整输入信息']);
        }
        //验证 提链日 y-n-j：前面不带0的日期
        if(!in_array(date('j'), json_decode($setting['tilian_days'],true))){
            apifasong(['zt'=>'0','msg'=>'还没到可提链日期']);
        }
        //验证 最低额
        if($appdata['num']<$setting['tilian_min']){
            apifasong(['zt'=>'0','msg'=>'单次提链数量最低'.$setting['tilian_min']]);
        }
        //验证 倍数
        if($appdata['num']%$setting['tilian_step']<>0){
            apifasong(['zt'=>'0','msg'=>'提链额度必须为'.$setting['tilian_step'].'的倍数']);
        }
        //验证 可提余额
        if($userinfo['ome1']<$appdata['num']){
            apifasong(['zt'=>'0','msg'=>'可提链OME余额不足']);
        }
        //验证 二级密码
        $userinfo['secpwd']=$userinfo['secpwd']?$userinfo['secpwd']:MD5($setting['nopwd']);
        if(MD5($appdata['secpwd'])<>$userinfo['secpwd']){
            apifasong(['zt'=>'0','msg'=>'支付密码错误']);
        }

        //计算手续费/矿工费
        $sxf=$appdata['num']*($setting['tilian_sxf']/100);  //手续费
        $kgf=$setting['tilian_kgf'];  //矿工费
        $omec=$appdata['num']-$sxf-$kgf;    //到账OMEC

        //月上限验证
        $yuechu=strtotime(date('Y-m-01'));
        $yuemo=strtotime(date('Y-m-t 23:59:59'));
        $tilian_yue=db('tilian')->where('uname',$userinfo['uname'])->where('status','neq','2')->where('time','between',[$yuechu,$yuemo])->sum('money');
        if($tilian_yue+$omec>$setting['tilian_maxyue']){
            apifasong(['zt'=>'0','msg'=>'超过本月提链上限'.$setting['tilian_maxyue'].'，您已提链'.$tilian_yue]);
        }

        //业务
        //-OME 
        Db::startTrans();   // 启动事务-------------
        $ret_ome1=ome1($userinfo,'-'.$appdata['num'],'tilian');
        $ret_ome=db('user')->where('id',$userinfo['id'])->dec('ome',($appdata['num']))->update();
        //-OME记录
        $note_sxf = $sxf?'，手续费'.$sxf:'';
        $note_kgf = $kgf?'，矿工费'.$kgf:'';
        $logidome=mylog('ome',$userinfo['uname'],'tilian','',$userinfo['ome'],'-'.$appdata['num'],'提链扣款'.$note_sxf.$note_kgf,'','');

        //插入提链任务（后台审核执行）
        $sis_user=userinfobyid('1');
        $sis_qb=db('adeth')->where('id',$sis_user['aid'])->find();
        $user_qb=db('adeth')->where('id',$userinfo['aid'])->find();
        $tilian['uid'] = $userinfo['id'];
        $tilian['uname'] = $userinfo['uname'];
        $tilian['money']=$omec;
        $tilian['time']=time();
        $tilian['oid']=$logidome;
        $tilian['from']=$sis_qb['address'];
        $tilian['to']=$user_qb['address'];
        $tilian['status']=0;

        $logidtilian=db('tilian')->insertGetId($tilian);

        if($ret_ome1 && $ret_ome && $logidome && $logidtilian){
            Db::commit();       // 提交事务-------------
            apifasong(['zt'=>'1','msg'=>'提链成功 请稍后查询到账情况']);
        }else{
            Db::rollback();     // 回滚事务-------------
            //插入错误记录
            $error['time']=date('Y-m-d H:i:s');
            $error['url']='/api/kuang.index/tilian:274';
            $error['note1']='提链失败';
            $error['note2']=$retqb.'-'.$userinfo['uname'];
            $error['note3']=$ret_ome1.'-'.$ret_ome.'-'.$logidome.'-'.$logidomec;
            db('error')->insertGetId($error);
            apifasong(['zt'=>'0','msg'=>'提链失败']);
        }
    }
    public function chicklv(){  //刷新用户等级
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        auto_uplv($userinfo['id']);

        $userinfo_now = userinfobyid($userinfo['id']);
        if($userinfo_now['lv']==$userinfo['lv']){
            apifasong(['zt'=>'0','msg'=>'当前等级 OM'.$userinfo_now['lv']]);
        }elseif($userinfo_now['lv']>$userinfo['lv']){
            apifasong(['zt'=>'1','msg'=>'恭喜你升级了！','userinfo'=>apiuserbaseinfo($userinfo_now)]);
        }else{
            apifasong(['zt'=>'-1','msg'=>'系统错误']);
        }
    }
    public function renwu_base(){  //任务挑战基本信息
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        $renwu_sis = db('renwu_sis')->where('zt',1)->select();
        $renwu_user = db('renwu_user')->where('uid',$userinfo['id'])->select();
        if($renwu_user){
            foreach ($renwu_user as &$value) {
                if($value['zt']==0){    //挑战中的 判断是否已超时
                    $litime = db('renwu_sis')->where('id',$value['rwid'])->value('litime');
                    if(time()-$value['btime']>=$litime){
                        db('renwu_user')->where('id',$value['id'])->update(['zt'=>2]);
                        $value['zt']=2;
                    }
                }
            }
            $renwu_user = array_column($renwu_user, NULL, 'rwid'); //rwid做健名 数据
        }
        apifasong([
            'zt'=>'1',
            'renwu_sis'=>$renwu_sis,
            'renwu_user'=>$renwu_user
        ]);
    }
    public function renwu_do(){  //任务挑战-开始挑战
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        
        //验证 必填项
        if(!isset($appdata['id'])){
            apifasong(['zt'=>'0','msg'=>'数据错误，请刷新再试']);
        }
        //合法性
        $renwu_sis = db('renwu_sis')->where('id',$appdata['id'])->find();
        if(!$renwu_sis || $renwu_sis['zt']<>1){
            apifasong(['zt'=>'0','msg'=>'任务不存在或已下架']);
        }
        if(db('renwu_user')->where('uid',$userinfo['id'])->where('rwid','in','1,2,3')->find()){
            apifasong(['zt'=>'0','msg'=>'你已挑战']);
        }
        if($userinfo['kuang']<$renwu_sis['chengfa']){
            apifasong(['zt'=>'0','msg'=>'矿金余额不足抵押']);
        }
        if($userinfo['lv']>=$renwu_sis['typeb']){
            apifasong(['zt'=>'0','msg'=>'你不能挑战改任务']);
        }

        Db::startTrans();   // 启动事务-------------
        $data['uid'] = $userinfo['id'];
        $data['uname'] = $userinfo['uname'];
        $data['rwid'] = $renwu_sis['id'];
        $data['btime'] = time();
        // $data['etime'] = 0;
        $data['zt'] = 0;
        $ret_id = db('renwu_user')->insertGetId($data);

        $ret_kuang = db('user')->where('id',$userinfo['id'])->setDec('kuang',$renwu_sis['chengfa']);
        $log_kuang = mylog('kuang',$userinfo['uname'],'tiaozhan','lv',$userinfo['kuang'],'-'.$renwu_sis['chengfa'],'任务挑战抵押',$ret_id,'');  //记录

        if($ret_id && $ret_kuang && $log_kuang){
            $renwu_user = db('renwu_user')->where('uid',$userinfo['id'])->select();
            if($renwu_user){
                $renwu_user = array_column($renwu_user, NULL, 'rwid'); //rwid做健名 数据
            }
            Db::commit();       // 提交事务-------------
            apifasong(['zt'=>'1','msg'=>'任务已启动','renwu_user'=>$renwu_user]);
        }else{
            Db::rollback();     // 回滚事务-------------
            apifasong(['zt'=>'0','msg'=>'任务启动失败']);
        }
    }

}






